<template>
  <div style=" margin:15px; margin-right:30px">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上游客户" prop="userCode">
            <el-select v-model="form.userCode" placeholder="请选择上游客户" clearable>
              <el-option
                v-for="item in parentData.upstreamCustomerList"
                :key="item.partyCode"
                :label="item.userName"
                :value="item.partyCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="货物类型" prop="coalBigType">
            <el-select
              v-model="form.coalBigType"
              placeholder="请选择货物类型(大)"
              clearable
              @change="changeCoalBigType"
            >
              <el-option
                v-for="item in parentData.goodsTypeList"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" label-width="30px">
          <el-form-item prop="coalType">
            <el-select v-model="form.coalType" placeholder="请选择货物类型(小)" clearable>
              <el-option
                v-for="item in goodsTypeList2"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="车型" prop="classificationCode">
            <el-select v-model="form.classificationCode" placeholder="请选择车型" clearable>
              <el-option
                v-for="item in parentData.vehicleDictList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="起始地" prop="shipperSubdivisionCode">
            <el-cascader
              v-model="shipperSubdivisionArray"
              :options="parentData.areaOptions"
              :props="{ value: 'countyCode', label: 'countyName', children: 'children' }"
              @change="changeShipperSubdivisionArray"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="目的地" prop="consigneeSubdivisionCode">
            <el-cascader
              v-model="consigneeSubdivisionArray"
              :options="parentData.areaOptions"
              :props="{ value: 'countyCode', label: 'countyName', children: 'children' }"
              @change="changeConsigneeSubdivisionArray"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="付费方式" prop="chargingType">
            <el-select v-model="form.chargingType" placeholder="请选择付费方式" clearable>
              <el-option
                v-for="item in chargingTypeOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="应收单价" prop="price">
            <el-input v-model="form.price" prop="address" placeholder="请输入应收单价" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="应收总价" prop="allFee">
            <el-input v-model="form.allFee" prop="address" placeholder="请输入应收总价" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
Vue.use(VXETable);
import { getUpstreamCustomer, updateUpstreamCustomer } from "@/api/shipment";
export default {
  name: "UpstreamCustomerEdit",
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          areaOptions: [],
          goodsTypeList: [],
          typeOptions: [],
          vehicleDictList: [],
          bankOptions: [],
          upstreamCustomerList: []
        };
      }
    }
  },
  data() {
    return {
      shipperSubdivisionArray: [],
      consigneeSubdivisionArray: [],
      goodsTypeList2: [],
      form: {},
      chargingTypeOptions: [
        { name: "单价吨/方/件", value: 0 },
        { name: "单价吨/方/件*公里数", value: 1 },
        { name: "总价", value: 2 }
      ],
      rules: {
        userCode: [
          { required: true, message: "请选择上游客户", trigger: "change" }
        ],
        coalBigType: [
          { required: true, message: "请选择货物类型（大）", trigger: "change" }
        ],
        coalType: [
          { required: true, message: "请选择货物类型（小）", trigger: "change" }
        ],
        classificationCode: [
          { required: true, message: "请选择车型", trigger: "change" }
        ],
        shipperSubdivisionCode: [
          { required: true, message: "请选择起始地", trigger: "change" }
        ],
        consigneeSubdivisionCode: [
          { required: true, message: "请选择目的地", trigger: "change" }
        ],
        chargingType: [
          { required: true, message: "请选择付费方式", trigger: "change" }
        ],
        price: [{ required: true, message: "请输入应收单价", trigger: "blur" }],
        allFee: [{ required: true, message: "请选择应收总价", trigger: "change" }]
      }
    };
  },
  methods: {
    // 获取 详情
    getInfo(code) {
      this.form = {}
      this.form.companyCode = this.$route.query.code ? this.$route.query.code : "";
      getUpstreamCustomer(code).then(response => {
        this.form = response.data;
        var item = this.parentData.goodsTypeList.find(item => item.code === response.data.coalBigType);
        this.goodsTypeList2 = (item && item.children) ? item.children : []
        this.shipperSubdivisionArray = response.data.originList;
        this.consigneeSubdivisionArray = response.data.destList;
      });
    },
    changeCoalBigType(val) {
      this.form.coalType = "";
      var item = this.parentData.goodsTypeList.find(item => item.code === val);
      if (item && item.children) {
        this.goodsTypeList2 = item.children;
      } else {
        this.goodsTypeList2 = [];
      }
    },
    funcSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const rLoading = this.openLoading();
          updateUpstreamCustomer(this.form)
            .then(response => {
              rLoading.close();
              this.$message({ message: response.msg, type: "success" });
              this.$emit("fatherEditMethod");
            })
            .then(() => this.$refs["form"].resetFields());
        } else {
          this.$message.error("必选项不能为空!");
        }
      });
    },
    funcCancel() {
      this.$emit("fatherCancelMethod", "edit");
    },
    changeShipperSubdivisionArray(value) {
      this.form.shipperSubdivisionCode = value[2];
    },
    changeConsigneeSubdivisionArray(value) {
      this.form.consigneeSubdivisionCode = value[2];
    }
  }
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  margin: 15px 0 15px 0;
  width: 175px;
  float: right;
}
.el-cascader {
  width: 100%;
}
.el-select {
  width: 100%;
}
</style>
